﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using System.IO;
using System.Xml.Serialization;
using System.Diagnostics;


namespace MG1
{
    public class Serializer
    {
        public GameSettingsSection config = new GameSettingsSection();
        string directory = "\\Base\\Config\\";
        string path=System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
        string fullPath;

        public Serializer()
        {
            fullPath = path + directory + "Config.xml";
        }

        public void LoadConfig()
        {
            XmlSerializer deserializer;
            TextReader textReader;

            if (File.Exists(fullPath))
            {
                deserializer = new XmlSerializer(typeof(GameSettingsSection));
                textReader = new StreamReader(fullPath);
                config = ((GameSettingsSection)deserializer.Deserialize(textReader));
                textReader.Close();
            }
        }

        public void SaveConfig(ConfigSection conf)
        {
            CreateDir(path+directory);
            XmlSerializer serializer = new XmlSerializer(typeof(GameSettingsSection));
            TextWriter writer = new StreamWriter(fullPath);
            serializer.Serialize(writer, config);
            writer.Close();
        }

        public void CreateDir(string _path)
        {
            if (!Directory.Exists(_path))
            {
                DirectoryInfo dir1 = new DirectoryInfo(_path);
                dir1.Create();
            }
        }


    }
}
